From 9524eae5a46285e574b869879fb0228fb4424a5f Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dave.stevenson@raspberrypi.com>
Date: Thu, 1 Dec 2022 13:54:49 +0000
Subject: [PATCH] media: i2c: ov7251: Add module param to select ext
 trig mode

As there isn't currently a defined mechanism for selecting an
external trigger mode on image sensors, copy the imx477
approach of using a module parameter to enable ext trig.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
---
 drivers/media/i2c/ov7251.c | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)

--- a/drivers/media/i2c/ov7251.c
+++ b/drivers/media/i2c/ov7251.c
@@ -23,6 +23,10 @@
 #include <media/v4l2-fwnode.h>
 #include <media/v4l2-subdev.h>
 
+static int trigger_mode;
+module_param(trigger_mode, int, 0644);
+MODULE_PARM_DESC(trigger_mode, "Set vsync trigger mode: 0=standalone, (1=source - not implemented), 2=sink");
+
 #define OV7251_SC_MODE_SELECT		0x0100
 #define OV7251_SC_MODE_SELECT_SW_STANDBY	0x0
 #define OV7251_SC_MODE_SELECT_STREAMING		0x1
@@ -525,7 +529,6 @@ static const struct reg_value ov7251_set
 	{ 0x3662, 0x01 },
 	{ 0x3663, 0x70 },
 	{ 0x3664, 0x50 },
-	{ 0x3666, 0x0a },
 	{ 0x3669, 0x1a },
 	{ 0x366a, 0x00 },
 	{ 0x366b, 0x50 },
@@ -592,9 +595,8 @@ static const struct reg_value ov7251_set
 	{ 0x3c00, 0x89 },
 	{ 0x3c01, 0x63 },
 	{ 0x3c02, 0x01 },
-	{ 0x3c03, 0x00 },
 	{ 0x3c04, 0x00 },
-	{ 0x3c05, 0x03 },
+	{ 0x3c05, 0x01 },
 	{ 0x3c06, 0x00 },
 	{ 0x3c07, 0x06 },
 	{ 0x3c0c, 0x01 },
@@ -624,6 +626,16 @@ static const struct reg_value ov7251_set
 	{ 0x5001, 0x80 },
 };
 
+static const struct reg_value ov7251_ext_trig_on[] = {
+	{ 0x3666, 0x00 },
+	{ 0x3c03, 0x17 },
+};
+
+static const struct reg_value ov7251_ext_trig_off[] = {
+	{ 0x3666, 0x0a },
+	{ 0x3c03, 0x00 },
+};
+
 static const unsigned long supported_xclk_rates[] = {
 	[OV7251_19_2_MHZ] = 19200000,
 	[OV7251_24_MHZ] = 24000000,
@@ -1372,6 +1384,23 @@ static int ov7251_s_stream(struct v4l2_s
 			dev_err(ov7251->dev, "could not sync v4l2 controls\n");
 			goto err_power_down;
 		}
+
+		/* Set vsync trigger mode */
+		switch (trigger_mode) {
+		case 2:
+			ov7251_set_register_array(ov7251,
+						  ov7251_ext_trig_on,
+						  ARRAY_SIZE(ov7251_ext_trig_on));
+			break;
+		case 0:
+		default:
+			/* case 1 for ext trig source currently not implemented */
+			ov7251_set_register_array(ov7251,
+						  ov7251_ext_trig_off,
+						  ARRAY_SIZE(ov7251_ext_trig_off));
+			break;
+		}
+
 		ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
 				       OV7251_SC_MODE_SELECT_STREAMING);
 		if (ret)
